<!-- 管理员增加成绩，肯定也需要成绩的id -->
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>成绩管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加成绩</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
  <el-form-item label="课程号" prop="courseId">
    <el-input v-model="ruleForm.courseId"></el-input>
  </el-form-item>
  <el-form-item label="课程名" prop="courseName">
    <el-input v-model="ruleForm.courseName"></el-input>
  </el-form-item>
  <el-form-item label="学生号" prop="studentId">
    <el-input v-model="ruleForm.studentId"></el-input>
  </el-form-item>
  <el-form-item label="学生名" prop="studentName">
    <el-input v-model="ruleForm.studentName"></el-input>
  </el-form-item>
  <el-form-item label="教师号" prop="teacherId">
    <el-input v-model="ruleForm.teacherId"></el-input>
  </el-form-item>
  <el-form-item label="教师名" prop="teacherName">
    <el-input v-model="ruleForm.teacherName"></el-input>
  </el-form-item>
  <el-form-item label="成绩" prop="score">
    <el-input v-model.number="ruleForm.score"></el-input>
  </el-form-item>

      
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即添加</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
     
    </el-form>
  </div>
</template>

<script>
export default {
  name: "InsertgradeInfo",
  data() {
    return {
      
      title: "添加成绩",
      ruleForm: { courseId:'',	
      courseName:'',
      studentId:'',
      studentName:'',	
      teacherId:'',
      teacherName:'',
    score:''},
    rules: {
  courseId: [{ required: true, message: "请填写课程号" }],
  courseName: [{ required: true, message: "请填写课程名" }],
  studentId: [{ required: true, message: "请填写学生号" }],
  studentName: [{ required: true, message: "请填写学生名" }],
  teacherId: [{ required: true, message: "请填写教师号" }],
  teacherName: [{ required: true, message: "请填写教师名" }],
  score: [{ required: true, message: "请填写成绩" }],
},
    };
  
  },
  created(){
    if(localStorage.getItem("type") !== 'admin'){this.$router.push(`/login`);this.$message.error('权限不够');}
  },
  methods: {
   
      //添加成绩信息
      async insertgradeInfo() {
      // JSON化
      const config = {
        headers: {
          'Content-Type': 'application/json',
          'token':`${localStorage.getItem("token")}`
        }
      };
      try {
        const { data: res } = await this.$http.post(`api/admin/grade/insert`,JSON.stringify(this.ruleForm),config);
        if (res.status !== 500) {
          this.$message.success('成功添加成绩信息');
          // 重新调用获取表格的代码
        } else {
          this.$message.error('添加失败');
        }
      } catch (error) {
        // Handle request error
        console.error('Request Error:', error);
        this.$message.error('添加失败');
    }
      },
      //提交表单信息
      submitForm(ruleForm) {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
            this.insertgradeInfo();
          }
          else{
            this.$message.error('请将信息填写完整');
          }
        });
      },
      // 重置清空表单信息
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
  
   
  }

</script>

<style lang="less" scoped>
.el-breadcrumb {
  padding-bottom: 10px;
}
.el-input {
  width: 40%;
}
/deep/ .el-textarea__inner {
  width: 40%;
  line-height: 3;
}
.el-form-item {
  margin: 18px 0 !important;
}
</style>
